package com.yiwei.zxp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

import com.allen.library.SuperButton;
import com.yiwei.zxp.R;
import com.yiwei.zxp.api.bean.GoodsTypeMoreBean;
import com.yiwei.zxp.api.model.CateGoryModel;
import com.yiwei.zxp.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * Created by xsooy-pc on 2017/10/23.
 */

public class GoodsTypeMoreAdapter extends ItemAdapter<CateGoryModel.DatasBean, GoodsTypeMoreAdapter.DefaultHolder> {

    private Context context;

    public GoodsTypeMoreAdapter(Context context) {
        super(context);
        this.context = context;
    }


    @Override
    public DefaultHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new DefaultHolder(inflate(R.layout.item_goods_type, parent));
    }

    @Override
    public void onBindViewHolder(DefaultHolder holder, int position) {
        CateGoryModel.DatasBean item = getItem(position);

        if (item.isCheck) {
            holder.btnType.setTextColor(0xffffffff);
            holder.btnType.setShapeSolidColor(0xffFF0030).setUseShape();
        } else {
            holder.btnType.setTextColor(0xff000000);
            holder.btnType.setShapeSolidColor(0xffEEEEEE).setUseShape();
        }
        holder.btnType.setText(item.catname);
        holder.btnType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (isIsOver(5) && !item.isCheck) {
                    ToastUtil.makeText(context, "不能超过5个");
                    return;
                }

                getmSource().get(position).isCheck = !getmSource().get(position).isCheck;
                notifyDataSetChanged();
            }
        });
    }

    public boolean isOneSelect() {
        for (int i = 0; i < getmSource().size(); i++) {
            if (getmSource().get(i).isCheck) {
                return true;
            }
        }
        return false;
    }

    public List<GoodsTypeMoreBean> getList() {
        List<GoodsTypeMoreBean> list = new ArrayList<>();
        GoodsTypeMoreBean bean;
        for (int i = 0; i < getmSource().size(); i++) {
            if (getmSource().get(i).isCheck) {
                bean = new GoodsTypeMoreBean(getmSource().get(i).id, getmSource().get(i).catname);
                list.add(bean);
            }
        }
        return list;
    }

    public boolean isIsOver(int num) {
        int numAdd = 0;
        for (int i = 0; i < getmSource().size(); i++) {
            if (getmSource().get(i).isCheck) {
                numAdd++;
            }
        }
        if (numAdd >= num) {
            return true;
        } else
            return false;

    }


    class DefaultHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.btn_type)
        SuperButton btnType;

        public DefaultHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }

    }


}
